package com.gitee.candy.encry;

import com.gitee.candy.string.StringUtils;

/**
 * 脱敏处理
 */
public class EncryptDesensitizeUtils {

    // 手机号
    public static String phone(String phone) {
        if (StringUtils.isBlank(phone)) {
            return null;
        }
        if ("null".equals(phone)) {
            return phone;
        }
        return phone.substring(0, 3) + "***" + phone.substring(phone.length() - 3);
    }

    // 银行卡号
    public static String bankCard(String bankCard) {
        if (StringUtils.isBlank(bankCard)) {
            return null;
        }
        if ("null".equals(bankCard)) {
            return bankCard;
        }
        return bankCard.substring(0, 4) + "*****" + bankCard.substring(bankCard.length() - 4);
    }

    // 电子邮箱
    public static String email(String email) {
        if (StringUtils.isBlank(email)) {
            return null;
        }
        if ("null".equals(email)) {
            return email;
        }
        if (email.contains("@")) {
            String[] split = email.split("@");
            return split[0].substring(0, 1) + "***" + split[0].substring(split[0].length() - 1) + "@" + split[1];
        } else {
            return email;
        }
    }

    // 身份证号
    public static String idCard(String idCard) {
        if (StringUtils.isBlank(idCard)) {
            return null;
        }
        if ("null".equals(idCard)) {
            return idCard;
        }
        return idCard.substring(0, 4) + "*****" + idCard.substring(idCard.length() - 4);
    }

    /**
     * 测试
     *
     * @param args
     */
    public static void main(String[] args) {
        System.out.println(phone("15920156459"));
        System.out.println(bankCard("6127000190005434866"));
        System.out.println(email("E-S70072@cdeledu.com"));
        System.out.println(idCard("130131198512161218"));
    }
}
